#include "drv_sht20.h"

sht20_val tem_hum_data;
void drv_sht20_init(void)
{
	drv_iic_init();

}

uint16_t drv_sht20_read(void)
{
	uint8_t data[2];
	uint16_t ret=0;
	drv_iic_start();
	drv_iic_senddata(0x80);
	if(drv_iic_waitACK()!=ACK)
	{
		drv_iic_stop();
		return NoACK;
	}

	drv_iic_senddata(0xe3);//获取温度
	if(drv_iic_waitACK()!=ACK)
	{
		drv_iic_stop();
		return NoACK;
	}

	drv_iic_start();
	drv_iic_senddata(0x81);
	if(drv_iic_waitACK()!=ACK)
	{
		drv_iic_stop();
		return NoACK;
	}
	delay_ms(85);
	data[0]=drv_iic_receivedata(ACK);// 接收高8位
	data[1]=drv_iic_receivedata(NoACK);// 接收低8位
	drv_iic_stop();
	printf("data[0]=%d data[2]=%d\r\n",data[0],data[1]);
	ret=((data[0]<<8)|data[1]);
	return ret;
}

uint8_t drv_sht20_reset(void)
{
	drv_iic_start();
	drv_iic_senddata(0x80);
	if(drv_iic_waitACK()!=ACK)
	{
		drv_iic_stop();
		return NoACK;
	}

	drv_iic_senddata(0xfe);//复位命令
	if(drv_iic_waitACK()!=ACK)
	{
		drv_iic_stop();
		return NoACK;
	}
	drv_iic_stop();
	return 0;
}

void drv_sht20_getdata(void)
{
	uint16_t temp=0;
	temp=drv_sht20_read();
	tem_hum_data.tem=-46.85+175.72*((float)temp/65536);
	printf("Temperature:%.2f\r\n",tem_hum_data.tem);
}
